import pygame
import random


# 初始化 pygame
pygame.init()

# 颜色
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)

# 屏幕尺寸
WIDTH, HEIGHT = 640, 480
CELL_SIZE = 20

# 初始化屏幕和时钟
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Snake Game")
clock = pygame.time.Clock()

class Snake:
    def __init__(self):
        self.body = [(5, 5), (4, 5), (3, 5)]
        self.direction = (1, 0)
        self.life = 3 # 蛇的生命值
        self.move_speed = 10 # 蛇的移动速度

    def move(self):
        head = self.body[0]
        new_head = (head[0] + self.direction[0], head[1] + self.direction[1])
        self.body.insert(0, new_head)
        self.body.pop()

    def grow(self):
        head = self.body[0]
        new_head = (head[0] + self.direction[0], head[1] + self.direction[1])
        self.body.insert(0, new_head)

    def draw(self, screen):
        for segment in self.body:
            pygame.draw.rect(screen, GREEN, (segment[0]*CELL_SIZE, segment[1]*CELL_SIZE, CELL_SIZE, CELL_SIZE))

def draw_countdown(screen, countdown):
    font = pygame.font.Font(None, 36)
    text = font.render(str(countdown), True, WHITE)
    text_rect = text.get_rect(center=(WIDTH // 2, HEIGHT // 2))
    screen.fill((0,0,0))  # 使用bacgkround填充屏幕，使得文字看起来更加突出色背景
    screen.blit(text, text_rect)
    pygame.display.flip()

# 弹出选择框，选择游戏难度
"""
# 设置字体
font = pygame.font.Font(None, 36)
# 创建按钮
easy_button = pygame.Rect(50, 50, 100, 50)
medium_button = pygame.Rect(50, 100, 100, 50)
hard_button = pygame.Rect(50, 150, 100, 50)

# 绘制按钮
pygame.draw.rect(screen, BLUE, easy_button)
pygame.draw.rect(screen, BLUE, medium_button)
pygame.draw.rect(screen, BLUE, hard_button)
# 显示文本
easy_text = font.render("容易", True, BLUE)
medium_text = font.render("中等", True, BLUE)
hard_text = font.render("困难", True, BLUE)
screen.blit(easy_text, (easy_button.x + 10, easy_button.y + 10))
screen.blit(medium_text, (medium_button.x + 10, medium_button.y + 10))
screen.blit(hard_text, (hard_button.x + 10, hard_button.y + 10))
# 更新屏幕显示
pygame.display.flip()

        # 检查鼠标点击事件
        elif event.type == pygame.MOUSEBUTTONDOWN:
            mouse_pos = event.pos
            if easy_button.collidepoint(mouse_pos):
                print("容易")
            elif medium_button.collidepoint(mouse_pos):
                print("中等")
            elif hard_button.collidepoint(mouse_pos):
                print("困难")
"""    

# 开始游戏的倒计时
countdown_time = 5  # 倒计时时间为5秒
draw_countdown(screen, countdown_time)
pygame.time.wait(1000)  # 等待1秒

while countdown_time > 0:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            quit()
    countdown_time -= 1
    draw_countdown(screen, countdown_time)
    pygame.time.wait(1000)  # 等待1秒

# 创建蛇和食物
snake = Snake()
food = (random.randint(0, (WIDTH//CELL_SIZE) - 1), random.randint(0, (HEIGHT//CELL_SIZE) - 1))

# 游戏主循环
running = True
while running:
    screen.fill(WHITE)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_UP and snake.direction != (0, 1):
                snake.direction = (0, -1)
            elif event.key == pygame.K_DOWN and snake.direction != (0, -1):
                snake.direction = (0, 1)
            elif event.key == pygame.K_LEFT and snake.direction != (1, 0):
                snake.direction = (-1, 0)
            elif event.key == pygame.K_RIGHT and snake.direction != (-1, 0):
                snake.direction = (1, 0)

    snake.move()

    # 检查蛇是否吃到食物
    if snake.body[0] == food:
        snake.grow()
        food = (random.randint(0, (WIDTH//CELL_SIZE) - 1), random.randint(0, (HEIGHT//CELL_SIZE) - 1))

    # 检查蛇是否碰到墙壁或自己
    if (snake.body[0][0] < 0 or snake.body[0][0] >= WIDTH//CELL_SIZE or
        snake.body[0][1] < 0 or snake.body[0][1] >= HEIGHT//CELL_SIZE or
        snake.body[0] in snake.body[1:]):
        running = False

    snake.draw(screen)
    pygame.draw.rect(screen, RED, (food[0]*CELL_SIZE, food[1]*CELL_SIZE, CELL_SIZE, CELL_SIZE))

    pygame.display.flip()
    clock.tick(10)

# 游戏结束
pygame.quit()
quit()
